package com.ymttest.business.service.shenqi;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ymttest.database.sqlwapper.shenqisettlement.OrderSettlementDetailWrapper;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONObject;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentPromoterProductBean;
import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentPromoterRelationSettlementBean;
import com.shop2cn.iapi.agentproducts.parameter.CatalogPriceDto;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.shenqisettlement.OrderSettlementDetail;
import com.ymttest.database.model.shenqisettlement.ProxyFissionCommisionRate;
import com.ymttest.database.model.shenqisettlement.ProxyFissionConfig;
import com.ymttest.database.model.shenqisettlement.ProxyFissionProductDetail;
import com.ymttest.database.model.shenqisettlement.ProxyFissionSettlementDetail;
import com.ymttest.database.mongo.ProxyOrderIndexQueryWapper;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sharding.model.RefundBillPo;
import com.ymttest.database.sharding.db.sharding.model.ShenqiProxyChainPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sqlwapper.shenqisettlement.ProxyFissionCommisionRateWrapper;
import com.ymttest.database.sqlwapper.shenqisettlement.ProxyFissionProductDetailWrapper;
import com.ymttest.database.sqlwapper.shenqisettlement.ProxyFissionSettlementDetailWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.YMTDateUtil;

public class ProxyFissionVerifyService {

	private static ProxyFissionCommisionRateWrapper proxyFissionCommisionRateWrapper = new ProxyFissionCommisionRateWrapper();
	private static ProxyFissionProductDetailWrapper proxyFissionProductDetailWrapper = new ProxyFissionProductDetailWrapper();
	private static ProxyFissionSettlementDetailWrapper proxyFissionSettlementDetailWrapper = new ProxyFissionSettlementDetailWrapper();

	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	private static OrderSettlementDetailWrapper orderSettlementDetailWrapper = new OrderSettlementDetailWrapper();

	public int getSettlementStatus(Boolean isCanceled, Boolean settled,
			Integer delayDays, List<RefundBillPo> refundBillPoList, Long orderId) {

		if (null != refundBillPoList) {

			if (null != refundBillPoList
					&& !RefundService.isEntireRefunded(orderId) && settled
					&& delayDays == 0)
				return 1;
			else if (null != refundBillPoList
					&& !RefundService.isEntireRefunded(orderId))
				return 0;
			else
				return 4;
		} else {

			if (isCanceled && !settled)
				return 4;
			else if (settled && delayDays == 0)
				return 1;
			else
				return 0;
		}
	}

	/**
	 * 获取AgentPromoteRelation
	 * 
	 * @param agentIdList
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static List<AgentPromoterRelationSettlementBean> getAgentPromoteRelationCall(
			List<Long> agentIdList) throws Exception {

		YmatouEasyCall getAgentPromoteRelationCall = new YmatouEasyCall("",
				"POST", "JSON");
		StringBuilder getAgentPromoteRelationStr = new StringBuilder(
				"{\"agentIdList\":[");

		for (Long agentId : agentIdList) {

			getAgentPromoteRelationStr.append(agentId + ",");
		}

		getAgentPromoteRelationStr.deleteCharAt(getAgentPromoteRelationStr
				.length() - 1);
		getAgentPromoteRelationStr.append("], ");
		getAgentPromoteRelationStr.append("\"maxLevel\":2}");

		getAgentPromoteRelationCall
				.setUrl("http://godmch.iapi.shop2cn.com/api/agent/getAgentPromoteRelation");

		getAgentPromoteRelationCall.setData(getAgentPromoteRelationStr
				.toString());
		getAgentPromoteRelationCall.callService();

		List<AgentPromoterRelationSettlementBean> agentPromoterRelationSettlementList = new ArrayList<>();

		Logger.comment(getAgentPromoteRelationCall.getReturnData());

		String returanData = getAgentPromoteRelationCall.getReturnData();
		JSONArray agentList = new JSONObject(returanData).getJSONObject(
				"result").getJSONArray("list");

		Long lowerProxyId = null;

		for (int i = 0; i < agentList.length(); i++) {

			JSONObject agentObject = agentList.getJSONObject(i);
			Long agentId = agentObject.getLong("agentId");

			JSONArray relations = agentObject.getJSONArray("relations");
			lowerProxyId = agentId;

			for (int j = 0; j < relations.length(); j++) {

				JSONObject relationObject = relations.getJSONObject(j);
				AgentPromoterRelationSettlementBean agentPromoterRelationSettlementBean = new AgentPromoterRelationSettlementBean();

				agentPromoterRelationSettlementBean.setAgentId(agentId);
				agentPromoterRelationSettlementBean.setAgentType(relationObject
						.getInt("agentType"));
				agentPromoterRelationSettlementBean.setInviterId(relationObject
						.getLong("inviterId"));
				agentPromoterRelationSettlementBean.setLayer(relationObject
						.getInt("level"));
				agentPromoterRelationSettlementBean
						.setLowerProxyId(lowerProxyId);

				agentPromoterRelationSettlementList
						.add(agentPromoterRelationSettlementBean);

				lowerProxyId = relationObject.getLong("inviterId");
			}
		}

		return agentPromoterRelationSettlementList;
	}

	public static List<AgentPromoterProductBean> buildAgentPromoterProductBean(
			OrderSettlementDetail orderSettlementDetail, Long proxyId,
			List<OrderDetailPo> orderDetailPoList, Integer baseCommissionType,
			ProxyFissionCommisionRate proxyFissionCommisionRate,
			BigDecimal proxyCommissionRate, Boolean isShenqiProxyBuyOrder)
			throws ClientProtocolException, IOException {

		ArrayList<AgentPromoterProductBean> agentPromoterProductBeanList = new ArrayList<>();

		Long goodsOwnerId = orderSettlementDetail.getGoods_owner_id();
		Long orderId = orderSettlementDetail.getOrder_id();

		for (OrderDetailPo orderDetailPo : orderDetailPoList) {

			String catalogId = orderDetailPo.getCatalogId();
			BigDecimal perProductBidAmount = BigDecimal.ZERO;

			// 盛付通为CNY
			String settlementCurrency = orderSettlementDetail
					.getPay_channel_type() == 1 ? "CNY"
					: GodmchManageCallService.getMchInfoBySellerId(proxyId)
							.getSettlementCurrency();

			BigDecimal bidVsSettlementExchangeRate = ShenqiSettlementService
					.getSellCurrencyVsBuyCurrencyRate(
							orderSettlementDetail.getBid_currency(),
							settlementCurrency);

			AgentPromoterProductBean agentPromoterProductBean = new AgentPromoterProductBean();

			BigDecimal salePrice = null;
			BigDecimal toChildPrice = null;

			if (isShenqiProxyBuyOrder) {

				CatalogPriceDto catalogPriceDto = SqTradingProductService
						.getPriceByCatalogIdList(Arrays.asList(catalogId),
								goodsOwnerId);

				// 直销价格
				salePrice = new BigDecimal(
						String.valueOf(catalogPriceDto.getDirectPrice()));
				toChildPrice = orderDetailPo.getProductPrice();
			} else {

				ShenqiProxyChainPo shenqiProxyChainPo = orderShardingWapper
						.selectProxyChainbyOrderIdAndCatalogIdAndSellerId(
								orderId, catalogId, goodsOwnerId);

				salePrice = shenqiProxyChainPo.getSalePrice();
				toChildPrice = shenqiProxyChainPo.getToChildPrice();
			}

			if (baseCommissionType == 1) {
				perProductBidAmount = salePrice.subtract(toChildPrice)
						.multiply(proxyCommissionRate);
			} else {
				perProductBidAmount = toChildPrice
						.multiply(proxyCommissionRate);
			}

			BigDecimal perProductSettleAmount = perProductBidAmount
					.multiply(bidVsSettlementExchangeRate);

			BigDecimal totalProductBidAmount = perProductBidAmount
					.multiply(new BigDecimal(orderDetailPo.getProductCount()));
			BigDecimal totalProductSettleAmount = perProductSettleAmount
					.multiply(new BigDecimal(orderDetailPo.getProductCount()));

			agentPromoterProductBean.setBaseCommissionType(baseCommissionType);
			agentPromoterProductBean.setGoodsOwnerId(goodsOwnerId);
			agentPromoterProductBean.setOrderDetailPo(orderDetailPo);
			agentPromoterProductBean
					.setPerProductBidAmount(perProductBidAmount);
			agentPromoterProductBean
					.setPerProductSettleAmount(perProductSettleAmount);
			agentPromoterProductBean
					.setProxyFissionCommisionRate(proxyFissionCommisionRate);
			agentPromoterProductBean.setProxyId(proxyId);
			agentPromoterProductBean.setSalePrice(salePrice);
			agentPromoterProductBean.setToChildPrice(toChildPrice);
			agentPromoterProductBean.setSettlementCurrency(settlementCurrency);
			agentPromoterProductBean
					.setBidVsSettlementExchangeRate(bidVsSettlementExchangeRate);
			agentPromoterProductBean
					.setProxyCommissionRate(proxyCommissionRate);
			agentPromoterProductBean
					.setTotalProductBidAmount(totalProductBidAmount);
			agentPromoterProductBean
					.setTotalProductSettleAmount(totalProductSettleAmount);

			agentPromoterProductBeanList.add(agentPromoterProductBean);
		}

		return agentPromoterProductBeanList;
	}

	public static HashMap<String, List<AgentPromoterRelationSettlementBean>> buildAgentPromoterRelation(
			OrderSettlementDetail orderSettlementDetail,
			ProxyFissionConfig proxyFissionConfig,
			List<OrderDetailPo> orderDetailPoList,
			Long userId,
			BigDecimal orderBidRealPayAmount,
			Long proxyFissionSellerId,
			Boolean isRefunded,
			HashMap<RefundBillPo, HashMap<Long, BigDecimal>> refundBillWithRefundedProfitMap,
			Boolean isShenqiProxyBuyOrder) throws Exception {

		ProxyFissionCommisionRate proxyFissionCommisionRate = null;
		int commissionType = proxyFissionConfig.getBase_commission_type();
		int delayDays = proxyFissionConfig
				.getProxy_fission_settlement_delay_days();

		BigDecimal commissionLimit = proxyFissionConfig.getCommision_limit();

		// 如果是内购订单，proxyFissionSellerId为订单userId，否则为订单sellerId
		List<AgentPromoterRelationSettlementBean> agentPromoterRelationSettlementBeanList = getAgentPromoteRelationCall(Arrays
				.asList(proxyFissionSellerId));
		List<AgentPromoterRelationSettlementBean> refundAgentPromoterRelationSettlementBeanList = new ArrayList<>();

		HashMap<String, List<AgentPromoterRelationSettlementBean>> agentPromoterRelationMap = new HashMap<>();

		for (AgentPromoterRelationSettlementBean agentPromoterRelationSettlementBean : agentPromoterRelationSettlementBeanList) {

			Long proxyId = agentPromoterRelationSettlementBean.getInviterId();
			Integer layer = agentPromoterRelationSettlementBean.getLayer();

			proxyFissionCommisionRate = proxyFissionCommisionRateWrapper
					.selectByLevelAndConfigId(
							(long) agentPromoterRelationSettlementBean.getAgentType(), proxyFissionConfig.getId());

			BigDecimal proxyCommissionRate = layer == 1 ? proxyFissionCommisionRate
					.getLayer1_rate() : proxyFissionCommisionRate.getLayer2_rate();

			List<AgentPromoterProductBean> agentPromoterProductBeanList = buildAgentPromoterProductBean(
					orderSettlementDetail, proxyId, orderDetailPoList, commissionType, proxyFissionCommisionRate,
					proxyCommissionRate, isShenqiProxyBuyOrder);

			BigDecimal sumBidSettlementAmount = agentPromoterProductBeanList
					.stream()
					.map(AgentPromoterProductBean::getTotalProductBidAmount)
					.reduce(BigDecimal.ZERO, BigDecimal::add);
			BigDecimal sumSettlementAmount = agentPromoterProductBeanList
					.stream()
					.map(AgentPromoterProductBean::getTotalProductSettleAmount)
					.reduce(BigDecimal.ZERO, BigDecimal::add);

			AgentPromoterProductBean productBean = agentPromoterProductBeanList
					.get(0);

			Boolean isShengPay = orderSettlementDetail.getPay_channel_type() == 1 ? true : false;

			String settlementCurrency = isShengPay ? "CNY" : productBean.getSettlementCurrency();
			String thirdPartyCurrency = isShengPay ? "CNY" : orderSettlementDetail.getForeign_currency();

			BigDecimal thirdPartyVsSettlementExchangeRate = ShenqiSettlementService
					.getSellCurrencyVsBuyCurrencyRate(thirdPartyCurrency, settlementCurrency);

			BigDecimal thirdPartyAmount = sumSettlementAmount.divide(
					thirdPartyVsSettlementExchangeRate, 2, BigDecimal.ROUND_DOWN);

			agentPromoterRelationSettlementBean
					.setAgentPromoterProductBeanList(agentPromoterProductBeanList);
			agentPromoterRelationSettlementBean
					.setPaymentId(orderSettlementDetail.getPayment_id());
			agentPromoterRelationSettlementBean
					.setProxyFissionCommisionRate(proxyFissionCommisionRate);
			agentPromoterRelationSettlementBean
					.setOriginProxyId(proxyFissionSellerId);
			agentPromoterRelationSettlementBean
					.setProxyFissionCommisionRate(proxyFissionCommisionRate);
			agentPromoterRelationSettlementBean.setUserId(userId);
			agentPromoterRelationSettlementBean
					.setOrderBidRealPayAmount(orderBidRealPayAmount);
			agentPromoterRelationSettlementBean.setSettlementStatus(0);
			agentPromoterRelationSettlementBean
					.setSumBidSettlementAmount(sumBidSettlementAmount);
			agentPromoterRelationSettlementBean
					.setSumSettlementAmount(sumSettlementAmount);
			agentPromoterRelationSettlementBean
					.setProxyCommissionRate(proxyCommissionRate);
			agentPromoterRelationSettlementBean.setBizId(orderSettlementDetail
					.getOrder_id() + "-" + proxyFissionSellerId);
			agentPromoterRelationSettlementBean.setSettlementType(0);
			agentPromoterRelationSettlementBean
					.setThirdPartyAmount(thirdPartyAmount);
			agentPromoterRelationSettlementBean
					.setThirdPartyCurrency(thirdPartyCurrency);
			agentPromoterRelationSettlementBean
					.setThirdPartyVsSettlementExchangeRate(thirdPartyVsSettlementExchangeRate);

			BigDecimal sellerTotalSalesAmount = new ProxyOrderIndexQueryWapper()
					.getSellerTotalSalesAmount(proxyId);

			int settlementStatus = 0;

			if (orderSettlementDetail.getSettlement_status() == 2) {
				settlementStatus = 2;
			} else if (orderSettlementDetail.getSettlement_status() == 1) {


				if (delayDays == 0
						&& !(sellerTotalSalesAmount.compareTo(commissionLimit) == -1)) {

					// 盛付通代理裂变结算，需要先分账，再进行结算
					if (orderSettlementDetail.getPay_channel_type() == 1) {

						ShenqiSettlementService.executeShengPayMerchantSharingConvergeTask();

						orderSettlementDetailWrapper.updateShengpaySharingExecuteStatusByOrderId(
								orderSettlementDetail.getOrder_id(), 1, 5);
						proxyFissionSettlementDetailWrapper.updateShengpaySharingExecuteStatusByOrderId(
								orderSettlementDetail.getOrder_id(), 1,  0);
						// 代理裂变结算
						ShenqiSettlementService.executeProxyFissionSettlementTask();
					}

					settlementStatus = 1;
				}

				if (sumSettlementAmount.compareTo(BigDecimal.ZERO) == 0)
					settlementStatus = 0;

				agentPromoterRelationSettlementBean
						.setExpectSettlementTime(YMTDateUtil
								.getBeforeOrNextDate(orderSettlementDetail
										.getSettlement_time(), delayDays));
			} else
				settlementStatus = 0;

			agentPromoterRelationSettlementBean
					.setSettlementStatus(settlementStatus);

			if (isRefunded) {

				if (settlementStatus == 1) {

					orderSettlementDetailWrapper.updateShengpaySharingExecuteStatusByOrderId(
							orderSettlementDetail.getOrder_id(), 1, 6);
					proxyFissionSettlementDetailWrapper.updateShengpaySharingExecuteStatusByOrderId(
							orderSettlementDetail.getOrder_id(), 1,  1);
					// 代理裂变结算
					ShenqiSettlementService.executeProxyFissionSettlementTask();
				}

				for (Map.Entry<RefundBillPo, HashMap<Long, BigDecimal>> entry : refundBillWithRefundedProfitMap
						.entrySet()) {

					AgentPromoterRelationSettlementBean refundBean = (AgentPromoterRelationSettlementBean) agentPromoterRelationSettlementBean
							.clone();

					RefundBillPo refundBillPo = entry.getKey();

					int refundProdCount = refundBillPo.getProductCount();
					String refundCatalogId = refundBillPo.getCatalogId();

					AgentPromoterProductBean agentPromoterProductBean = agentPromoterProductBeanList
							.stream()
							.filter(bean -> bean.getOrderDetailPo()
									.getCatalogId()
									.equalsIgnoreCase(refundCatalogId))
							.collect(Collectors.toList()).get(0);

					BigDecimal refundBidAmount = agentPromoterProductBean
							.getPerProductBidAmount()
							.multiply(new BigDecimal(refundProdCount))
							.setScale(2, BigDecimal.ROUND_UP);

					BigDecimal refundSettlementAmount  = refundBidAmount.multiply(
							agentPromoterProductBean.getBidVsSettlementExchangeRate())
							.setScale(2, BigDecimal.ROUND_HALF_UP);

					refundBean.setSumBidSettlementAmount(refundBidAmount
							.negate());
					refundBean.setSumSettlementAmount(refundSettlementAmount
							.negate());
					refundBean.setBizId(orderSettlementDetail.getOrder_id()
							+ "-" + proxyFissionSellerId + "-"
							+ refundBillPo.getRefundBillNo());

					refundBean.setSettlementType(1);

					refundAgentPromoterRelationSettlementBeanList
							.add(refundBean);
				}
			}
		}

		agentPromoterRelationMap.put("settlement",
				agentPromoterRelationSettlementBeanList);
		agentPromoterRelationMap.put("refund",
				refundAgentPromoterRelationSettlementBeanList);

		return agentPromoterRelationMap;
	}

	public static void verifyProxyFissionSettlementDetail(
			OrderSettlementDetail orderSettlementDetail,
			AgentPromoterRelationSettlementBean agentPromoterRelationSettlementBean) {

		Logger.comment("验证proxy_fission_settlement_detail表");

		Long orderId = orderSettlementDetail.getOrder_id();
		Long inviterId = agentPromoterRelationSettlementBean.getInviterId();

		int settlementType = agentPromoterRelationSettlementBean
				.getSettlementType();

		ProxyFissionCommisionRate proxyFissionCommisionRate = agentPromoterRelationSettlementBean
				.getProxyFissionCommisionRate();
		List<AgentPromoterProductBean> agentPromoterProductBeanList = agentPromoterRelationSettlementBean
				.getAgentPromoterProductBeanList();

		AgentPromoterProductBean agentPromoterProductBean = agentPromoterProductBeanList
				.get(0);

		ProxyFissionSettlementDetail actualResult = proxyFissionSettlementDetailWrapper
				.selectByOrderIdAndProxyIdAndBizId(orderId, inviterId,
						agentPromoterRelationSettlementBean.getBizId(),
						agentPromoterRelationSettlementBean.getSettlementType());

		String bidCurrency = orderSettlementDetail.getBid_currency();

		Logger.comment("验证分销裂变结算明细：orderId：" + orderId + "，inviterId："
				+ inviterId + "，结算单类型："
				+ agentPromoterRelationSettlementBean.getSettlementType());

		Logger.verifyEquals(orderSettlementDetail.getPayment_id(),
				actualResult.getPayment_id(), "验证Payment_id");
		Logger.verifyEquals(agentPromoterRelationSettlementBean.getUserId(),
				actualResult.getUser_id(), "验证User_id");
		Logger.verifyEquals(orderSettlementDetail.getGoods_owner_id(),
				actualResult.getGoods_owner_id(), "验证Goods_owner_id");
		Logger.verifyEquals(inviterId, actualResult.getProxy_id(), "验证Proxy_id");
		Logger.verifyEquals(
				proxyFissionCommisionRate.getProxy_fission_config_id(),
				actualResult.getProxy_fission_config_id(),
				"验证Proxy_fission_config_id");
		Logger.verifyEquals(agentPromoterRelationSettlementBean.getLayer(),
				actualResult.getProxy_level_index(), "验证Proxy_level_index");
		Logger.verifyEquals(proxyFissionCommisionRate.getProxy_level_label(),
				actualResult.getProxy_level_label(), "验证Proxy_level_label");
		DataUtil.verifyDecimalEquals(
				agentPromoterRelationSettlementBean.getProxyCommissionRate(),
				actualResult.getProxy_commision_rate(),
				"验证Proxy_commision_rate");
		Logger.verifyEquals(bidCurrency, actualResult.getBid_currency(),
				"验证Bid_currency");
		DataUtil.verifyDecimalEquals(
				agentPromoterRelationSettlementBean.getSumBidSettlementAmount(),
				actualResult.getBid_settlement_amount(),
				"验证Bid_settlement_amount");
		DataUtil.verifyDecimalEquals(
				agentPromoterProductBean.getBidVsSettlementExchangeRate(),
				actualResult.getBid_vs_settlement_exchange_rate(),
				"验证Bid_vs_settlement_exchange_rate");
		Logger.verifyEquals(agentPromoterProductBean.getSettlementCurrency(),
				actualResult.getSettlement_currency(), "验证Settlement_currency");
		Logger.verifyEquals(agentPromoterRelationSettlementBean
				.getSumSettlementAmount().setScale(2, BigDecimal.ROUND_HALF_UP),
				actualResult.getSettlement_amount(), "验证Settlement_amount");
		Logger.verifyEquals(
				agentPromoterRelationSettlementBean.getSettlementStatus(),
				actualResult.getSettlement_status(), "验证Settlement_status");

		if (agentPromoterRelationSettlementBean.getSettlementStatus() == 1)
			DataUtil.verifyDateNearlyEquals(agentPromoterRelationSettlementBean
					.getExpectSettlementTime(), actualResult
					.getSettlement_time(), 2000, "验证Settlement_time");
		else
			Logger.verifyEquals(null, actualResult.getSettlement_time(),
					"验证Settlement_time");

		Logger.verifyEquals(orderSettlementDetail.getPay_channel_type(),
				actualResult.getPay_channel_type(), "验证Pay_channel_type");
		Logger.verifyEquals(
				orderSettlementDetail.getCustomer_payment_order_no(),
				actualResult.getCustomer_payment_order_no(),
				"验证Customer_payment_order_no");
		DataUtil.verifyDecimalEquals(
				orderSettlementDetail.getCustomer_payment_order_amount(),
				actualResult.getCustomer_payment_order_amount(),
				"验证Customer_payment_order_amount");
		Logger.verifyEquals(agentPromoterProductBean.getBaseCommissionType(),
				actualResult.getBase_commission_type(),
				"验证Base_commission_type");

		// 程序有问题，未修复，现在的needChange跟着orderSettlement
		Logger.verifyEquals(
				agentPromoterRelationSettlementBean.getThirdPartyCurrency()
						.equalsIgnoreCase(
								agentPromoterProductBean
										.getSettlementCurrency()) ? false
						: true, actualResult.getNeed_exchange(),
				"验证Need_exchange");
		DataUtil.verifyDecimalEquals(
				agentPromoterRelationSettlementBean.getOrderBidRealPayAmount(),
				actualResult.getOrder_bid_real_pay_amount(),
				"验证Order_bid_real_pay_amount");
		Logger.verifyEquals(
				agentPromoterRelationSettlementBean.getLowerProxyId(),
				actualResult.getLower_proxy_id(), "验证Lower_proxy_id");
		Logger.verifyEquals(settlementType,
				actualResult.getProxy_settlement_type(),
				"验证Proxy_settlement_type");
		DataUtil.verifyDecimalEquals(agentPromoterRelationSettlementBean
				.getThirdPartyVsSettlementExchangeRate(), actualResult
				.getThird_party_vs_settlement_exchange_rate(),
				"验证Third_party_vs_settlement_exchange_rate");
		DataUtil.verifyDecimalEquals(
				agentPromoterRelationSettlementBean.getThirdPartyAmount(),
				actualResult.getThird_party_amount(), "验证Third_party_amount");
		Logger.verifyEquals(
				agentPromoterRelationSettlementBean.getThirdPartyCurrency(),
				actualResult.getThird_party_currency(),
				"验证Third_party_currency");
		Logger.verifyEquals(
				agentPromoterRelationSettlementBean.getOriginProxyId(),
				actualResult.getOrigin_proxy_id(), "验证Origin_proxy_id");
		// Logger.verifyEquals(true, actualResult.getHp_settled(),
		// "验证Hp_settled");

		DataUtil.verifyDateNearlyEquals(
				agentPromoterRelationSettlementBean.getExpectSettlementTime(),
				actualResult.getExpected_settlement_time(), 2000,
				"验证Expected_settlement_time");

		Logger.verifyEquals(agentPromoterRelationSettlementBean.getBizId(),
				actualResult.getBiz_id(), "验证Biz_id");
	}

	public static void verifyProxyFissionProductDetail(
			OrderSettlementDetail orderSettlementDetail,
			AgentPromoterProductBean agentPromoterProductBean, Integer layer) {

		Logger.comment("验证proxy_fission_product_detail表");

		OrderDetailPo orderDetailPo = agentPromoterProductBean
				.getOrderDetailPo();
		ProxyFissionCommisionRate proxyFissionCommisionRate = agentPromoterProductBean
				.getProxyFissionCommisionRate();

		Long orderId = orderDetailPo.getOrderId();
		Long proxyId = agentPromoterProductBean.getProxyId();
		String catalogId = orderDetailPo.getCatalogId();

		ProxyFissionProductDetail actualResult = proxyFissionProductDetailWrapper
				.selectByOrderIdAndProxyIdAndCatalogId(orderId, proxyId,
						catalogId);

		Logger.comment("验证分销裂变商品明细：orderId：" + orderId + "，catalogId："
				+ catalogId);

		Logger.verifyEquals(orderSettlementDetail.getPayment_id(),
				actualResult.getPayment_id(), "验证Payment_id");
		Logger.verifyEquals(orderSettlementDetail.getPay_channel_type(),
				actualResult.getPay_channel_type(), "验证Pay_channel_type");
		Logger.verifyEquals(agentPromoterProductBean.getGoodsOwnerId(),
				actualResult.getGoods_owner_id(), "验证Goods_owner_id");
		Logger.verifyEquals(proxyId, actualResult.getProxy_id(), "验证Proxy_id");
		Logger.verifyEquals(
				proxyFissionCommisionRate.getProxy_fission_config_id(),
				actualResult.getProxy_fission_config_id(),
				"验证Proxy_fission_config_id");
		Logger.verifyEquals(layer, actualResult.getProxy_level_index(),
				"验证Proxy_level_index");
		Logger.verifyEquals(proxyFissionCommisionRate.getProxy_level_label(),
				actualResult.getProxy_level_label(), "验证Proxy_level_label");
		DataUtil.verifyDecimalEquals(
				agentPromoterProductBean.getProxyCommissionRate(),
				actualResult.getProxy_commision_rate(),
				"验证Proxy_commision_rate");
		DataUtil.verifyDecimalEquals(agentPromoterProductBean.getSalePrice(),
				actualResult.getGoods_owner_product_price(),
				"验证Goods_owner_product_price");
		DataUtil.verifyDecimalEquals(
				agentPromoterProductBean.getToChildPrice(),
				actualResult.getProxy_product_price(), "验证Proxy_product_price");
		Logger.verifyEquals(orderDetailPo.getProductCount(),
				actualResult.getProduct_num(), "验证Product_num");
		Logger.verifyEquals(orderSettlementDetail.getBid_currency(),
				actualResult.getBid_currency(), "验证Bid_currency");
		Logger.verifyEquals(agentPromoterProductBean.getSettlementCurrency(),
				actualResult.getSettlement_currency(), "验证Settlement_currency");
		DataUtil.verifyDecimalEquals(
				agentPromoterProductBean.getBidVsSettlementExchangeRate(),
				actualResult.getBid_vs_settlement_exchange_rate(),
				"验证Bid_vs_settlement_exchange_rate");
		DataUtil.verifyDecimalEquals(
				agentPromoterProductBean.getPerProductSettleAmount(),
				actualResult.getPer_product_settle_amount(),
				"验证Per_product_settle_amount");
		DataUtil.verifyDecimalEquals(
				agentPromoterProductBean.getPerProductBidAmount(),
				actualResult.getPer_product_bid_amount(),
				"验证Per_product_bid_amount");
		Logger.verifyEquals(agentPromoterProductBean.getBaseCommissionType(),
				actualResult.getBase_commission_type(),
				"验证Base_commission_type");
		Logger.verifyEquals(orderDetailPo.getProductId(),
				actualResult.getProduct_id(), "验证Product_id");
	}

	public static void main(String[] args) throws Exception {

		ArrayList<Long> list = new ArrayList<>();
		list.add(501401140L);

		ProxyFissionVerifyService.getAgentPromoteRelationCall(list);
	}
}
